<?php

class Fpt_Business_Block_block_adapter_redis extends Fpt_Business_Block_block_adapter_abstract
{

    /**
     * @var thethao_modules_layout_business_block_adapter_redis Singleton instance
     */
    protected static $_instance;
    /**
     * String detail of 1 layout 
     */
    const BlockDetail = "block_detail";
    const SHARDING_DB = 0;
    const SHARDING_ALL = 0;
    

    /**
     *
     * @return thethao_modules_layout_business_block_adapter_redis
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

   /**
     * Return key for type & arg in layout only
     * @param string $type
     * @param string $args
     * @return string
     * @throws Core_Exception 
     */
    public function getKeyByType($type, $args)
    {
        try
        {
            $keyCaching = '';
            switch ($type)
            {
                case self::BlockDetail:
                    $keyCaching = Fpt_Global::makeCacheKey(self::BlockDetail, $args);
                    break;
                default :
                    throw new Core_Exception('wrong key', -1);
                    break;
            }
            return $keyCaching;
        }
        catch(Exception $ex)
        {
             throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }

    /**
     * Add layout
     * @param array $params
     * @return type
     * @throws Core_Exception 
     */
    public function addBlock($params, $isUpdate = false)
    {
        try
        {
        
            $rs = false;
            //get redis storage
            $redisInstance = Fpt_Global::getRedisInstance(self::SHARDING_DB, self::SHARDING_ALL);
            //make redis key
            $keyBlock = self::getKeyByType(self::BlockDetail, $params['BlockName']);
           
            //check exists
            $isExists = $redisInstance->hGetAll($keyBlock);
            
            //try to add layout exists
            if(!empty($isExists) && $isUpdate == false && !is_array($params))
            {
                return false;
            }
           
            //okie then add detail
            $rs = $redisInstance->hMset($keyBlock, $params);
        } 
        catch (Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return $rs;
    }
    
    /**
     *
     * @param type $params
     * @return type
     * @throws Core_Exception 
     */
    public function getBlock($params)
    {
        try
        {
            $rs = false;
            //get instance redis
            $redisInstance = Fpt_Global::getRedisInstance(self::SHARDING_DB, self::SHARDING_ALL);
            //make redis key
            $keyBlock = Fpt_Global::makeCacheKey(self::BlockDetail, $params['BlockName']);

            //get layout detail
            $rs = $redisInstance->hGetAll($keyBlock);         
        } 
        catch (Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return $rs;
    }
    
    /**
     * Get List Layout
     * @return type
     * @throws Core_Exception 
     */
    public function getListBlock()
    {
        try
        {
            $rs = false;
            //get instance redis
            $redisInstance = Fpt_Global::getRedisInstance(self::SHARDING_DB, self::SHARDING_ALL);
            //make redis key
            $keyLayout = Fpt_Global::makeCacheKey(self::BlockDetail, '*');

            //get all keys layout name
            $rs = $redisInstance->keys($keyLayout);
            //remove prefix
            array_walk($rs, 'thethao_library_global::removeKeyPrefix', self::BlockDetail);
        } 
        catch (Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return $rs;
    }

    
}